package app.hzvmap.com.rangecontrol.utils;


import org.xutils.DbManager;
import org.xutils.db.table.TableEntity;
import org.xutils.x;

/**
 * Created by weiguo on 2017/02/20.
 */
public class DbHelper {

    private static DbManager db;

    public static DbManager getDb() {
        if (db == null) {
            //本地数据的初始化
            DbManager.DaoConfig daoConfig = new DbManager.DaoConfig()
                    .setDbName("range") //设置数据库名
                    .setDbVersion(18) //设置数据库版本,每次启动应用时将会检查该版本号,
                    //发现数据库版本低于这里设置的值将进行数据库升级并触发DbUpgradeListener
                    .setAllowTransaction(true)//设置是否开启事务,默认为false关闭事务
                    .setTableCreateListener(new DbManager.TableCreateListener() {
                        @Override
                        public void onTableCreated(DbManager db, TableEntity<?> table) {

                        }

                    })//设置数据库创建时的Listener
                    .setDbOpenListener(new DbManager.DbOpenListener() {
                        @Override
                        public void onDbOpened(DbManager db) {
                            // 开启WAL, 对写入加速提升巨大
                            db.getDatabase().enableWriteAheadLogging();
                        }
                    })
                    .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
                        @Override
                        public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
                            try {
                                if (oldVersion != newVersion) {
//                                    if (oldVersion <= 8) {
//                                        db.addColumn(WpUnit4App.class, "ThinClassName");
//                                        db.addColumn(WpUnit4App.class, "SmallClassName");
////                                        db.addColumn(TreeType.class, "NurseryParam");
////                                        db.addColumn(TreeType.class, "IsLocation");
////                                        db.addColumn(TreeType.class, "GrowthHabit");
////                                        db.addColumn(TreeType.class, "MorphologicalCharacter");
////                                        db.addColumn(TreeType.class, "Pics");
////
////                                        db.addColumn(NurseryRequest.class, "newid");
////                                        db.execNonQuery("update nursery set newid = id");
////                                        db.addColumn(TreeRequest.class, "newid");
////                                        db.execNonQuery("update tree set newid = id");
////
////                                        db.addColumn(NurseryRequest.class, "LocationStr");
//                                    }
                                }
                            } catch (Exception e) {

                            }
                            // db.addColumn(...);
                            // db.dropTable(...);
                            // ...
                            // or
                            // db.dropDb();
                        }
                    });//设置数据库升级时的Listener,这里可以执行相关数据库表的相关修改,比如alter语句增加字段等
            //.setDbDir(null);//设置数据库.db文件存放的目录,默认为包名下databases目录下
            db = x.getDb(daoConfig);
        }
        return db;
    }
}
